import {computed, ref, unref} from "vue";
import { defineStore } from 'pinia';
import {formatPrice} from "@/utils/format.js"
import {add as xe_add,multiply as xe_multiply} from 'xe-utils'

const cartList = ref([])
export const useCartStore = defineStore('cart', () => {
	
	const goodsTotal = computed(()=>{
		return unref(cartList).reduce((prev,current)=>{
			return xe_add(prev,Number(current._countNum))
		},0)
	})
	
	const priceTotal = computed(()=>{
		let amount =  unref(cartList).reduce((prev,current)=>{
			const result = xe_multiply(Number(current._countNum),Number(current._skuInfo.price))
			return xe_add(prev,result)
		},0)
		return formatPrice(amount)
	})
	
	const pushGoods = (data)=>{
		let {_id,_skuInfo:{_id:skuId},_countNum} = data;
		
		const index =  unref(cartList).findIndex(item=>_id && skuId && (item._id==_id) && (item._skuInfo._id == skuId))
		if(index>-1){
			cartList.value[index]._countNum = Number(_countNum) + Number(cartList.value[index]._countNum)
			return;
		}
		cartList.value.unshift(data)
	}
	
	return {
		cartList,
		pushGoods,
		goodsTotal,
		priceTotal
	}
});
